/*
 * Copyright (c) 2022-2023 cetcclout Co., Ltd All rights reserved.
 * Sensor Adapter is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package com.cetccloud.cimp.common.util;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.json.JSONUtil;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WeatherJtCreator {
	
	private static final Logger log = LoggerFactory.getLogger(WeatherJtCreator.class);
	
    public static void main(String[] args) {
        Double v =-8.2;
        System.out.println(floatWdDot5(v+""));
    }
    /**
     * 上下浮动20%
     */
    public static String float20(String value){
        double c = RandomUtil.randomDouble(-0.2, 0.2,2, RoundingMode.FLOOR);
        Double dValue = Double.valueOf(value);
        dValue= dValue+dValue*c;
        if(String.valueOf(dValue).contains(".")) {
            String data = String.format("%.2f", Double.valueOf(dValue));
            return data;
        }
        return dValue+"";
    }
    public static String floatWdDot5(String value){
        double c = RandomUtil.randomDouble(-0.5, 0.5,2, RoundingMode.FLOOR);
        Double dValue = Double.valueOf(value);
        dValue= dValue+c;
        if(String.valueOf(dValue).contains(".")) {
            String data = String.format("%.2f", Double.valueOf(dValue));
            return data;
        }
        return dValue+"";
    }
    //地面气压 上下浮动0.002
    public static String float002Qy(String value){
        double c = RandomUtil.randomDouble(-0.002, 0.002,5, RoundingMode.FLOOR);
        Double dValue = Double.valueOf(value);
        dValue= dValue+dValue*c;
        if(String.valueOf(dValue).contains(".")) {
            String data = String.format("%.2f", Double.valueOf(dValue));
            return data;
        }
        return dValue+"";
    }


    /**
     * 上下浮动20%
     */
    public static String int20(String value){
        double c = RandomUtil.randomDouble(-0.2, 0.2,2, RoundingMode.FLOOR);
        Integer dValue = Integer.valueOf(value);
        dValue= dValue + (int)(dValue*c);
        return dValue+"";
    }

    public static String ssFxInt20_0_360(String value){
        //瞬时风向 不超过0-360
        double c = RandomUtil.randomDouble(-0.2, 0.2,2, RoundingMode.FLOOR);
        Integer dValue = Integer.valueOf(value);
        dValue= dValue + (int)(dValue*c);
        if(dValue>360){
            dValue = 360;
        }
        if(dValue<0){
            dValue = 0;
        }
        return dValue+"";
    }

    /**
     * a01007	I2800300R	风速
     * a01001	I2800000R	温度
     * a01008	I2800400R	风向
     * a01002	I2800100R	湿度
     * a06001	I2800500R	降水量
     * a01006	I2800200R	气压
     * 温度	地面气压	相对湿度	瞬时风向	瞬时风速	1小时极大风速	1小时降水	10分钟平均能见度
     * 5.9	1007.3	 29	    185	    8.3	      10.8	      0	            14.256
     * @param deviceId
     * @param dataBlock
     * @param row
     */
    public static void createQiXiangRtdData(String deviceId, StringBuilder dataBlock, Map<String, Object> row) {
        String wendu_a01001 = floatWdDot5(row.get("温度").toString());
        String dmqy_a01006 = float002Qy(row.get("地面气压").toString());
        String xdsd_a01002 = int20(row.get("相对湿度").toString());
        String ssfx_a01008 = ssFxInt20_0_360(row.get("瞬时风向").toString());
        String ssfs_a01007 = float20(row.get("瞬时风速").toString());
        String jsl1hour_a06001 = row.get("1小时降水").toString();
        jsl1hour_a06001 = jsl1hour_a06001.equals("0")?"0":float20(row.get("1小时降水").toString());

        dataBlock.append("a01001-Rtd="+ wendu_a01001+",a01001-Flag=N;");
        dataBlock.append("a01006-Rtd="+ dmqy_a01006+",a01006-Flag=N;");
        dataBlock.append("a01002-Rtd="+ xdsd_a01002+",a01002-Flag=N;");
        dataBlock.append("a01008-Rtd="+ ssfx_a01008+",a01008-Flag=N;");
        dataBlock.append("a01007-Rtd="+ ssfs_a01007+",a01007-Flag=N;");
        dataBlock.append("a06001-Rtd="+ jsl1hour_a06001+",a06001-Flag=N");

        List<Double> a01001Rtd = DataConfig.MINUTE_DATA.get(deviceId,"a01001-Rtd")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a01001-Rtd");
        a01001Rtd.add(Double.valueOf(wendu_a01001));
        DataConfig.MINUTE_DATA.put(deviceId, "a01001-Rtd", a01001Rtd);

        List<Double> a01006Rtd = DataConfig.MINUTE_DATA.get(deviceId,"a01006-Rtd")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a01006-Rtd");
        a01006Rtd.add(Double.valueOf(dmqy_a01006));
        DataConfig.MINUTE_DATA.put(deviceId, "a01006-Rtd", a01006Rtd);

        List<Double> a01002Rtd = DataConfig.MINUTE_DATA.get(deviceId,"a01002-Rtd")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a01002-Rtd");
        a01002Rtd.add(Double.valueOf(xdsd_a01002));
        DataConfig.MINUTE_DATA.put(deviceId, "a01002-Rtd", a01002Rtd);

        List<Double> a01008Rtd = DataConfig.MINUTE_DATA.get(deviceId,"a01008-Rtd")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a01008-Rtd");
        a01008Rtd.add(Double.valueOf(ssfx_a01008));
        DataConfig.MINUTE_DATA.put(deviceId, "a01008-Rtd", a01008Rtd);

        List<Double> a01007Rtd = DataConfig.MINUTE_DATA.get(deviceId,"a01007-Rtd")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a01007-Rtd");
        a01007Rtd.add(Double.valueOf(ssfs_a01007));
        DataConfig.MINUTE_DATA.put(deviceId, "a01007-Rtd", a01007Rtd);

        List<Double> a06001Rtd = DataConfig.MINUTE_DATA.get(deviceId,"a06001-Rtd")==null?new ArrayList<>():DataConfig.MINUTE_DATA.get(deviceId,"a06001-Rtd");
        a06001Rtd.add(Double.valueOf(jsl1hour_a06001));
        DataConfig.MINUTE_DATA.put(deviceId, "a06001-Rtd", a06001Rtd);

    }
    public static void createQiXiangMinuteData(String deviceId, StringBuilder dataBlock){
        dataBlock.append(getDataMinute(deviceId,"a01001-Rtd"));
        dataBlock.append(getDataMinute(deviceId,"a01006-Rtd"));
        dataBlock.append(getDataMinute(deviceId,"a01002-Rtd"));
        dataBlock.append(getDataMinute(deviceId,"a01008-Rtd"));
        dataBlock.append(getDataMinute(deviceId,"a01007-Rtd"));
        dataBlock.append(getDataMinute(deviceId,"a06001-Rtd").replace(";",""));

        DataConfig.MINUTE_DATA.get(deviceId,"a01001-Rtd").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a01006-Rtd").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a01002-Rtd").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a01008-Rtd").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a01007-Rtd").clear();
        DataConfig.MINUTE_DATA.get(deviceId,"a06001-Rtd").clear();

        log.info("十分钟数据：{}", JSONUtil.toJsonStr(DataConfig.MINUTE_DATA));
    }
    public static void createQiXiangHourData(String deviceId, StringBuilder dataBlock){
        dataBlock.append(getDataHour(deviceId,"a01001-Rtd"));
        dataBlock.append(getDataHour(deviceId,"a01006-Rtd"));
        dataBlock.append(getDataHour(deviceId,"a01002-Rtd"));
        dataBlock.append(getDataHour(deviceId,"a01008-Rtd"));
        dataBlock.append(getDataHour(deviceId,"a01007-Rtd"));
        dataBlock.append(getDataHour(deviceId,"a06001-Rtd").replace(";",""));

        //处理完毕数据后清空
        DataConfig.HOUR_DATA.get(deviceId,"a01001-Rtd").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a01006-Rtd").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a01002-Rtd").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a01008-Rtd").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a01007-Rtd").clear();
        DataConfig.HOUR_DATA.get(deviceId,"a06001-Rtd").clear();

        log.info("小时数据：{}",JSONUtil.toJsonStr(DataConfig.HOUR_DATA));
    }
    public static String getDataHour(String deviceId, String ID){
        List<Double> rtdDataList = DataConfig.HOUR_DATA.get(deviceId,ID);
        //得到和
        double cou = rtdDataList.stream().mapToDouble( Double :: valueOf ).sum();
        //得到平均数
        double avg = rtdDataList.stream().mapToDouble( Double :: valueOf ).average().getAsDouble();
        //得到最小值
        double min = rtdDataList.stream().mapToDouble( Double :: valueOf ).min().getAsDouble();
        //得到最大值
        double max = rtdDataList.stream().mapToDouble( Double :: valueOf ).max().getAsDouble();
        String finalId = ID.replace("-Rtd","");
        String data = finalId+"-Cou="+ getValue(cou) +","+ finalId +"-Min="+  getValue(min) +","+ finalId +"-Avg="+  getValue(avg) +","+ finalId +"-Max="+  getValue(max) +";";
        return data;
    }
    private static String getCellValue(Object sValue){
        String value = sValue.toString()+"";
        if(value.contains(".")) {
            String data = String.format("%.3f", Double.valueOf(value));
            return data;
        }else{
            return value;
        }
    }
    public static String getDataMinute(String deviceId, String ID){
        List<Double> rtdDataList = DataConfig.MINUTE_DATA.get(deviceId,ID);

        List<Double> hourData = DataConfig.HOUR_DATA.get(deviceId,ID)==null?new ArrayList<>():DataConfig.HOUR_DATA.get(deviceId,ID);
        hourData.addAll(rtdDataList);
        DataConfig.HOUR_DATA.put(deviceId,ID,hourData);
        //得到和
        double cou = rtdDataList.stream().mapToDouble( Double :: valueOf ).sum();
        //得到平均数
        double avg = rtdDataList.stream().mapToDouble( Double :: valueOf ).average().getAsDouble();
        //得到最小值
        double min = rtdDataList.stream().mapToDouble( Double :: valueOf ).min().getAsDouble();
        //得到最大值
        double max = rtdDataList.stream().mapToDouble( Double :: valueOf ).max().getAsDouble();
        String finalId = ID.replace("-Rtd","");
        String data = finalId+"-Cou="+ getValue(cou) +","+ finalId +"-Min="+ getValue(min) +","+ finalId +"-Avg="+ getValue(avg) +","+ finalId +"-Max="+ getValue(max) +";";
        return data;
    }
    private static String getValue(double value){
        String v = value+"";
        return getCellValue(v);
    }
}
